<?php
/**
 * 管理组模型
 * @author hedong <2015-12-07 16:31:45>
 */
namespace OrderAdmin\Model;

use Carbon\Carbon;
use Think\Model\RelationModel;

class OrderModel extends RelationModel {


    /**
     * 获取总数
     * @return mixed
     */
    function getAllCount(){
        return $this->count();
    }

    /**
     * 获取当月总数
     * @return mixed
     */
    function getCountThisMonth(){
        $startTime = Carbon::now()->firstOfMonth()->toDateTimeString();
        $endTime = Carbon::now()->firstOfMonth()->addMonth(1)->toDateTimeString();
        return $this->where(['add_time'=>['between', [$startTime, $endTime]]])->count();
    }

    /**
     * 获取进行中数量
     * @return mixed
     */
    public function getDoingCount(){
        return $this->where(['order_status'=>['neq', 7]])->count();
    }

    /**
     * 获取已完成数量
     * @return mixed
     */
    public function getFinishCount(){
        return $this->where(['order_status' => 7])->count();
    }

    /**
     * 分页查询
     * @param $param
     * @return array
     */
    function getData($param){
        // 请求参数
        if($param['key']){
            $where['a.order_no'] = ['like', '%'.$param['key'].'%'];
            $where['b.store_name'] = ['like', '%'.$param['key'].'%'];
            $where['b.manager_name'] = ['like', '%'.$param['key'].'%'];
            $where['c.name'] = ['like', '%'.$param['key'].'%'];
            $where['d.name'] = ['like', '%'.$param['key'].'%'];
            $where['e.name'] = ['like', '%'.$param['key'].'%'];
        }
        if($param['worker_id']){
            $where2['a.worker_id'] = $param['worker_id'];
        }
        if($param['activity_id']){
            $where2['a.activity_id'] = $param['activity_id'];
        }
        if($param['worker_order_status']){
            $where2['a.worker_order_status'] = $param['worker_order_status'];
        }
        if($param['order_status']){
            $where2['a.order_status'] = ['in', explode(',', $param['order_status'])];
        }
        if($param['store_id']){
            $where2['a.store_id'] = $param['store_id'];
        }
        if($param['search_order_status'] || $param['search_order_status']==='0'){
            $where2['a.order_status'] = $param['search_order_status'];
        }
        if($where && $where2){
            $where['_logic'] = 'or';
            $where2['_complex'] = $where;
        }elseif($where && !$where2){
            $where['_logic'] = 'or';
            $where2 = $where;
        }elseif(!$where && !$where2){
            $where2 = [];
        }

        // 获取数据
        $pageNum = ($param['page'] - 1) * $param['size'];
        $data['count'] = $this->alias('a')
            ->where($where2)
            ->join('LEFT JOIN __STORE__ b ON a.store_id = b.id')
            ->join('LEFT JOIN __AUDIT__ c ON a.audit_user_id = c.id')
            ->join('LEFT JOIN __ACTIVITY__ d ON a.activity_id = d.id')
            ->join('LEFT JOIN __PARTNER__ e ON a.partner_id = e.id')
            ->count();
        if($data['count'] < $pageNum){
            return ['status' => false, 'msg'=>'没有更多数据了'];
        }
        $data['data'] = $this->alias('a')
            ->field('a.id, a.order_no, a.order_status, a.worker_order_status, b.store_name, b.manager_name, c.name audit_name, d.name activity_name, e.name partner_name')
            ->join('LEFT JOIN __STORE__ b ON a.store_id = b.id')
            ->join('LEFT JOIN __AUDIT__ c ON a.audit_user_id = c.id')
            ->join('LEFT JOIN __ACTIVITY__ d ON a.activity_id = d.id')
            ->join('LEFT JOIN __PARTNER__ e ON a.partner_id = e.id')
            ->where($where2)
            ->order('a.add_time desc')
            ->limit($pageNum, $param['size'])
            ->select();

        return ['status'=> true, 'data'=> $data];
    }

    /**
     * 获取订单详情
     * @param $order_no
     * @return bool
     */
    public function getInfoByOrderNo($order_no){
        $data = $this->alias('a')
            ->field('a.*,
            b.name activity_name, b.up_start_time, b.up_end_time,
            c.store_name, c.manager_name, c.mobile store_mobile, concat_ws("", c.province, c.city, c.area, c.address) detail_address,
            d.name audit_name, d.mobile audit_mobile, 
            e.name partner_name, e.mobile partner_mobile')
            ->join('LEFT JOIN __ACTIVITY__ b ON a.activity_id = b.id')
            ->join('LEFT JOIN __STORE__ c ON a.store_id = c.id')
            ->join('LEFT JOIN __AUDIT__ d ON a.audit_user_id = d.id')
            ->join('LEFT JOIN __PARTNER__ e ON a.partner_id = e.id')
            ->where(['a.order_no'=>$order_no])->find();

        $data['ads'] = M('order_ads')->where(['order_no'=>$order_no])->order('id asc')->select();
        foreach ($data['ads'] as &$v){
            $v['ads_detail'] = M('order_ads_detail')->where(['order_ads_id'=>$v['id']])->order('id asc')->select();
        }
        unset($v);
        return $data ? : false;
    }

    /**
     * 删除订单
     * @param $id
     * @return bool
     */
    public function deleteById($id){
        M()->startTrans();
        $res1 = M('order_ads_detail')->where(['order_id'=>$id])->delete();
        $res2 = M('order_ads')->where(['order_id'=>$id])->delete();
        $res3 = M('order_status_log')->where(['order_id'=>$id])->delete();
        $res4 = $this->where(['id'=>$id])->delete();
        if($res1!==false && $res2!==false && $res3!==false && $res4){
            M()->commit();
            return true;
        }else{
            M()->rollback();
            return false;
        }
    }
}
